/*!
// Testweb v0.4.0
// http://or-change.cn
// Copyright 2014 OrChange Inc. All rights reserved.
// Licensed under the GPL License.
*/
/*jslint vars: true, forin: true, plusplus: true */
/*global define: false */
define(function (require, exports, module) {
	'use strict';
	var OC = require("lib/core/oc"),
		jQuery = require("lib/jquery/jquery");

	exports.execute = function (me, $, load) {
		var id, fn, player_statue = "stop",
			url_consle = "static";
		return function () {
			fn = {
				toggle: function () {
					var isOpen = load.OC.cfg("UI_CONSOLE_OPEN");
					jQuery("#OC").animate({
						right: (isOpen ? "-455px" : "0px")
					});
					load.OC.cfg("UI_CONSOLE_OPEN", !isOpen);
				},
				block: function () {
					jQuery("#mask").css("display", "block");
				},
				ready: function () {
					jQuery("#mask").css("display", "none");
				},
				refresh_url: function () {
					var old_url;
					id = load.tpm.getTemplate(me).setInterval(1500, function () {
						var location, i = 0;
						location = jQuery("iframe").get(0).contentWindow.location.href;
						if (location !== old_url) {
							old_url = location;
							$("#iframe-location").val(location);
						}
					});
				}
			};

			// 修改步骤 ==============================================
			$("[data-sid] a").click(function () {
				var step_index = $(this).parents("[data-sid]").attr("data-sid");
				load.tpm.open("step", {
					action: {
						method: "modify"
					},
					step_index: step_index,
					form: load.ppm.getStepSerialization(step_index)
				});
			});
			$("tr[data-sid]").jeegoocontext('edit-menu', {
				fadeIn: 0,
				onSelect: function (e, context) {
					var method = $(this).attr("id"),
						index = $(context).attr("data-sid");
					switch (method) {
					case "insert-up":
						load.tpm.open("step", {
							form: {},
							action: {
								method: "addUp",
								index: index
							}
						});
						break;
					case "insert-down":
						load.tpm.open("step", {
							form: {},
							action: {
								method: "addDown",
								index: index
							}
						});
						break;
					}
				}
			});

			// 菜单功能 ==============================================
			$(".lang").click(function () {
				var lang = $(this).attr("id");
				load.OC.cfg("CURRENT_LANG", lang);
				load.tpm.refreshAll();
			});
			$("#config").click(function () {
				load.tpm.open("config", {});
			});

			// 主面板功能 ==============================================
			$(".handle").click(function () {
				fn.toggle();
			});
			$("#open").click(function () {
				var data;
				data = load.ppm.getProcessList("local");
				load.dim.query("listProcess", OC.getToken(), function (list_process) {
					data.remote = list_process;
					load.tpm.open("process_open", data);
				});
			});
			$("#new,#test-script").click(function () {
				load.tpm.open("process", {
					method: "new",
					hasDictionary: false,
					form: {}
				});
			});
			$("#step").click(function () {
				var data = {
					form: {},
					action: {
						method: "new"
					},
					hasDictionary: load.ppm.hasDictionary()
				};
				if (data.hasDictionary) {
					data.keys = load.ppm.getDictionaryKeys();
				}
				load.tpm.open("step", data);
			});
			$("#hightlight").click(function () {
				var flag = load.OC.cfg("SHOW_AREA_MODE");
				load.OC.cfg("SHOW_AREA_MODE", !flag);
			});
			$("#edit").click(function () {
				load.tpm.open("process", {
					method: "modify",
					form: load.ppm.getProcessSerialization(),
					hasDictionary: load.ppm.hasDictionary()
				});
			});
			$("#dictionary").click(function () {
				load.tpm.open("dictionary_pattern", {
					form: {},
					options: {}
				});
			});

			// 步骤队列编辑方法 ==============================================
			$(".delete").click(function () {
				var step_index = $(this).parents("[data-sid]").attr("data-sid");
				load.ppm.removeStep(step_index);
				load.tpm.refresh(me, load.ppm.getProgramList());
			});
			$(".up").click(function () {
				var step_index = $(this).parents("[data-sid]").attr("data-sid");
				load.ppm.moveStepUp(step_index);
				load.tpm.refresh(me, load.ppm.getProgramList());
			});
			$(".down").click(function () {
				var step_index = $(this).parents("[data-sid]").attr("data-sid");
				load.ppm.moveStepDown(step_index);
				load.tpm.refresh(me, load.ppm.getProgramList());
			});

			// 播放器控制逻辑 ==============================================
			$("#play").click(function () {
				if (player_statue === "stop") {
					load.ppm.play();
				}
				if (player_statue === "pause") {
					load.ppm.resume();
				}
				$(this).hide();
				$("#pause").show();
				$(".bar-close").click();
				player_statue = "play";
				fn.block();
				fn.toggle();
			});
			$("#pause").click(function () {
				if (player_statue !== "play") {
					return;
				}
				$(this).hide();
				$("#play").show();

				load.ppm.pause();
				player_statue = "pause";
			});
			$("#stop").click(function () {
				if (player_statue === "stop") {
					return;
				}
				load.ppm.stop();
				player_statue = "stop";
				$("#pause").hide();
				$("#play").show();
				fn.ready();
			});

			// iframe控制逻辑 ==============================================
			$("#refresh").click(function () {
				window[0].location.reload();
			});
			$("#modity").click(function () {
				if (url_consle !== "static") {
					return;
				}
				url_consle = "edit";
				clearInterval(id);
				$("#iframe-location").removeAttr("readonly").focus();
				$(this).hide();
				$("#goto").show();
			});
			$("#goto").mouseenter(function () {
				url_consle = "goto";
			});
			$("#goto").mouseleave(function () {
				if (url_consle === "static") {
					return false;
				}
				url_consle = "edit";
			});
			$("#iframe-location").blur(function () {
				var url;
				if (url_consle === "static") {
					return;
				}
				url = $("#iframe-location").attr("readonly", "").val();
				if (url_consle === "goto") {
					jQuery("iframe").attr("src", url);
				}
				url_consle = "static";
				fn.refresh_url();
				$("#goto").hide();
				$("#modity").show();
			});
			fn.refresh_url();
		};
	};
});